﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BotAdapter;
using Yeti_v0._01.App_code;

namespace Yeti_v0._01
{
    public partial class Conversation : System.Web.UI.Page
    {
        private BotAdapter.BotAdapter yeti;
        public string txt, empty = "";   
        private string userName;

        [Serializable]
        public struct OneShot
        {
            
            public string pattern { get; set; }
            public string userInput { get; set; }
            public string response { get; set; }
        }


        public List<OneShot> QaList
        {
            get
            {
                if (this.ViewState["QaList"] == null)
                {
                    this.ViewState["QaList"] = new List<OneShot>();
                }
                return (List<OneShot>)(this.ViewState["QaList"]);
            }
            set { this.ViewState["QaList"] = value; }
  
        }      

        public SerializableSession serializableSession
        {
            get
            {
                if (this.ViewState["serializableSession"] == null)
                {
                    this.ViewState["serializableSession"] = new SerializableSession();
                }
                return (SerializableSession)(ViewState["serializableSession"]);
            }
            set { this.ViewState["serializableSession"] = value; }

        }
        public bool isFirst
        {
            get
            {
                if (this.ViewState["isFirst"] == null)
                {
                    this.ViewState["isFirst"] = true;
                }
                return (bool)(this.ViewState["isFirst"]);
            }
            set { this.ViewState["isFirst"] = value; }

        }
      

        protected void Page_Load(object sender, EventArgs e)
        {
            question_input1.Focus();
           
            var botPath = System.IO.Path.Combine(Server.MapPath(""),
                                                     System.IO.Path.Combine("config", "Settings.xml"));
            
           // userName = Request.ServerVariables["REMOTE_USER"];
            userName = "User";

            yeti = new BotAdapter.BotAdapter(userName, botPath);  
            if (isFirst)
            {
                serializableSession.turns = new List<SerializableTurn>();
                serializableSession.startDate = DateTime.Now;
                serializableSession.userIp = Page.Request.UserHostAddress;
                serializableSession.userName = userName;
                //sessionLog = new SessionLog(Page.Request.UserHostAddress, userName);
                isFirst = false;
            }
           
        }
     

        protected void AddQuestion(string answer, string botInput)
        {
            string inputValue = "<user> " + answer;
            System.Web.UI.HtmlControls.HtmlGenericControl createDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
            createDiv.Attributes["class"] = "answer";
            createDiv.InnerHtml = answer;
           // createDiv.Controls.Add(AddBotInput(botInput));
            conversation.Controls.Add(createDiv);
        }

        protected void AddAnswer(string question)
        {
            string inputValue = "<user> " + question;
            System.Web.UI.HtmlControls.HtmlGenericControl createDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
            createDiv.Attributes["class"] = "question";
            createDiv.InnerHtml = question;
            conversation.Controls.Add(createDiv);
        }
        protected System.Web.UI.HtmlControls.HtmlGenericControl AddBotInput(string input)
        {
            System.Web.UI.HtmlControls.HtmlGenericControl createDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
            
            createDiv.ID = "pattern";
            createDiv.InnerHtml = input;

            return createDiv;
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
           

            //initiating of serializable object which represent Turn
            var serializableTurn = new SerializableTurn();

            var conversationStage = yeti.Chat(question_input1.Text);
         
            //adding values to structure
            var oneShot = new OneShot();
            oneShot.pattern = conversationStage.pattern;
            oneShot.userInput = conversationStage.userInput;
            oneShot.response = conversationStage.response;

            //adding this structure to list
            QaList.Add(oneShot);

            int counter = 1;
            //Showing all Turns of Conversation 
            foreach (var q in QaList)
            {
                AddQuestion(q.userInput, q.pattern);
                AddAnswer(q.response);

                //Writing info to serializable turn
                serializableTurn.dateStamp = DateTime.Now;
                serializableTurn.patternContent = q.pattern;
                serializableTurn.userInput = q.userInput;
                serializableTurn.responseContent = q.response;

                //adding turn to serializable session if it is new turn (need to check if number of turns is smaller than counter)
                if(counter > serializableSession.turns.Count)
                {               
                    serializableSession.turns.Add(serializableTurn);                   
                }

                counter++;

            }

            question_input1.Text = "";
        }

        protected void sendLogButton_Click(object sender, EventArgs e)
        {
            //getting context
            yetiEntities yE = new yetiEntities();
          

            //filling session with all data from serializableSession !!!
            SessionLog sL = new SessionLog(serializableSession.userIp, serializableSession.userName, serializableSession.startDate);
            sL.FillSessionLogWithAllData(serializableSession.turns,yE);
            
        }

        protected void clearConversationButton_Click(object sender, EventArgs e)
        {
            QaList = null;
            serializableSession = null;         
            isFirst = true;
            question_input1.Text = "";
        }

  
       
    }
}